package com.summer.peony.myinterface.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.summer.peony.common.entity.AjaxResult;
import com.summer.peony.common.entity.AjaxResult.Type;
import com.summer.peony.myinterface.pojo.SServer;
import com.summer.peony.myinterface.service.SServerService;

@RestController
@RequestMapping("/server")
public class SServerController {

	@Autowired
	private SServerService sServerService;

	/**
	 * @Description: TODO(新增服务器地址)
	 * @param sServer
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2020年12月22日 上午10:17:03
	 * @throws:
	 */
	@PostMapping("/addSServer")
	public AjaxResult addSServer(@RequestBody SServer sServer) {
		AjaxResult ajaxResult = new AjaxResult();
		Integer myInf = sServerService.addSServer(sServer);
		if (myInf == 1) {
			ajaxResult.put("code", 0);
			ajaxResult.put("msg", "添加成功");
			return ajaxResult;
		}
		ajaxResult.put("code", 999999);
		ajaxResult.put("msg", "添加失败");
		return ajaxResult;
	}

	/**
	 * @Description: TODO(修改服务器地址)
	 * @param sServer
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2020年12月22日 上午10:17:22
	 * @throws:
	 */
	@PutMapping("/updateSServer")
	public AjaxResult updateSServer(@RequestBody SServer sServer) {
		AjaxResult ajaxResult = new AjaxResult();
		Integer myInf = sServerService.updateSServer(sServer);
		if (myInf == 1) {
			ajaxResult.put("code", 0);
			ajaxResult.put("msg", "修改成功");
			return ajaxResult;
		}
		ajaxResult.put("code", 999999);
		ajaxResult.put("msg", "修改失败");
		return ajaxResult;
	}

	/**
	 * @Description: TODO(删除服务器地址)
	 * @param id
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2020年12月22日 上午10:17:35
	 * @throws:
	 */
	@DeleteMapping("/deleteSServer/{id}")
	public AjaxResult deleteSServer(@PathVariable("id") Integer id) {
		Integer myInf = sServerService.deleteSServer(id);
		if (myInf == 1) {
			return new AjaxResult(Type.SUCCESS, "删除成功");
		}
		return new AjaxResult(Type.ERROR, "删除失败");
	}

	/**
	 * @Description: TODO(查询服务器列表)
	 * @param name
	 * @param use
	 * @param pageNum
	 * @param pageSize
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2020年12月22日 上午10:18:12
	 * @throws:
	 */
	@GetMapping("/selectAllSServer")
	public AjaxResult selectAllSServer(String name, Integer uuse, Integer pageNum, Integer pageSize) {
		PageInfo<SServer> pageInfo = sServerService.selectAllSServer(name, uuse, pageNum, pageSize);
		return new AjaxResult(Type.SUCCESS, "查询成功", pageInfo);
	}

	/**
	 * @Description: TODO(修改选择哪个服务器)
	 * @param id
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2020年12月22日 上午11:32:12
	 * @throws:
	 */
	@GetMapping("/updateByUuse/{id}")
	public AjaxResult updateByUse(@PathVariable("id") Integer id) {
		Integer itg = sServerService.updateByUuse(id);
		if (itg == 1) {
			return new AjaxResult(Type.SUCCESS, "选择成功");
		}
		return new AjaxResult(Type.ERROR, "选择失败");
	}
}
